from rest_framework import serializers
from Task import models
from django.conf import settings
from django_celery_beat.models import PeriodicTask, CrontabSchedule, IntervalSchedule, SolarSchedule  # 倒入插件model
from django_celery_results.models import TaskResult  # 执行结果
import pytz  # 时区模块


# 任务列表
class PeriodicTaskSerializer(serializers.ModelSerializer):
    interval_info = serializers.SerializerMethodField(read_only=True)
    crontab_info = serializers.SerializerMethodField(read_only=True)
    solar_info = serializers.SerializerMethodField(read_only=True)

    def get_interval_info(self, obj):
        myobj = obj.interval
        MODELS_CHOICES = settings.MODELS_CHOICES['task']['period']
        if myobj:
            period = [chices[1] for chices in MODELS_CHOICES if chices[0] == myobj.period][0]
            return {
                'id': myobj.id,
                'every': myobj.every,
                'period': period,
                'value': '{every}{period}'.format(every=myobj.every, period=period)
            }
        else:
            return myobj

    def get_crontab_info(self, obj):
        myobj = obj.crontab
        MODELS_CHOICES = settings.MODELS_CHOICES['task']['timezone']
        if myobj:
            if myobj.timezone:
                timezone = [chices[1] for chices in MODELS_CHOICES if chices[0] == myobj.timezone.zone][0]
            else:
                timezone = myobj.timezone
            return {
                'id': myobj.id,
                'timezone': timezone,
                'month_of_year': myobj.month_of_year,
                'day_of_month': myobj.day_of_month,
                'day_of_week': myobj.day_of_week,
                'hour': myobj.hour,
                'minute': myobj.minute,
                'value': '{timezone}时区 {month_of_year}年{day_of_month}月{day_of_week}星期{hour}时{minute}分'.format(
                    timezone=timezone,
                    month_of_year=myobj.month_of_year,
                    day_of_month=myobj.day_of_month,
                    day_of_week=myobj.day_of_week,
                    hour=myobj.hour,
                    minute=myobj.minute
                )
            }
        else:
            return myobj

    def get_solar_info(self, obj):
        myobj = obj.solar
        if myobj:
            event = myobj.event
            latitude = myobj.latitude
            longitude = myobj.longitude
            return {
                'id': myobj.id,
                'event': event,
                'latitude': latitude,
                'longitude': longitude,
                # 'value': '{event} {latitude} {longitude}'.format(event=str(event),latitude=str(latitude),longitude=str(longitude)),
                'value': '{event} {latitude} {longitude}'.format(event=event, latitude=latitude,
                                                                 longitude=longitude)
            }
        else:
            return myobj

    class Meta:
        model = PeriodicTask
        fields = '__all__'


# 计划任务定义
class CrontabScheduleSerializer(serializers.ModelSerializer):
    timezone = serializers.SerializerMethodField(read_only=True)
    value = serializers.SerializerMethodField(read_only=True)
    count = serializers.SerializerMethodField(read_only=True)

    def get_count(self, obj):
        myobj = obj.periodictask_set
        return myobj.count()

    def get_value(self, obj):
        MODELS_CHOICES = settings.MODELS_CHOICES['task']['timezone']
        timezone = [chices[1] for chices in MODELS_CHOICES if chices[0] == obj.timezone.zone][0]
        return '{timezone} {month_of_year}年{day_of_month}月{day_of_week}星期{hour}时{minute}分'.format(
            timezone=timezone,
            month_of_year=obj.month_of_year,
            day_of_month=obj.day_of_month,
            day_of_week=obj.day_of_week,
            hour=obj.hour,
            minute=obj.minute
        )

    def get_timezone(self, obj):
        myobj = obj.timezone
        if isinstance(myobj, str):
            return myobj
        else:
            return myobj.zone

    class Meta:
        model = CrontabSchedule
        fields = '__all__'
        # fields = ['timezone']
        extra_kwargs = {
            "timezone": {"write_only": True},
        }


# 间隔任务
class IntervalScheduleSerializer(serializers.ModelSerializer):
    value = serializers.SerializerMethodField(read_only=True)
    count = serializers.SerializerMethodField(read_only=True)

    def get_count(self, obj):
        myobj = obj.periodictask_set
        return myobj.count()

    def get_value(self, obj):
        MODELS_CHOICES = settings.MODELS_CHOICES['task']['period']
        period = [chices[1] for chices in MODELS_CHOICES if chices[0] == obj.period][0]
        return '{every}{period}'.format(every=obj.every, period=period)

    class Meta:
        model = IntervalSchedule
        fields = '__all__'


# 根据太用升起、降落定制任务（无用）
class SolarScheduleSerializer(serializers.ModelSerializer):
    value = serializers.SerializerMethodField(read_only=True)
    count = serializers.SerializerMethodField(read_only=True)

    def get_count(self, obj):
        myobj = obj.periodictask_set
        return myobj.count()

    def get_value(self, obj):
        event = obj.event
        latitude = obj.latitude
        longitude = obj.longitude
        return '{event} {latitude} {longitude}'.format(event=event, latitude=latitude, longitude=longitude)

    class Meta:
        model = SolarSchedule
        fields = '__all__'


# 任务执行结果
class TaskResultSerializer(serializers.ModelSerializer):
    class Meta:
        model = TaskResult
        fields = '__all__'


class MyShellSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.MyShell
        fields = '__all__'
